<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="searchHandle">
      <el-form-item>
        <el-dropdown
          size="large"
          type="primary"
          placement="bottom-start"
          :hide-on-click="true"
          trigger="click"
          @command="handleCommand" >
          <el-input
            min="120px"
            max="120px"
            :readonly= true
            :value="table_name"
            placeholder='点击选择表'
            suffix-icon='el-icon-arrow-down'
            class="el-dropdown-link">
          </el-input>
          <el-dropdown-menu class="project-dropdown" style="height: 320px" slot="dropdown">
            <div >
            <el-dropdown-item v-for="item in tableList['tableNamesCH']" :key="item" :command=item>{{item}}</el-dropdown-item>
            <el-dropdown-item divided command='bDefault' >默认</el-dropdown-item></div>
          </el-dropdown-menu>
        </el-dropdown>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchHandle(table_name)">查询</el-button>
      </el-form-item>
    </el-form>
    <el-collapse-transition>
    <div v-if="is_down">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="">
        <el-form-item>
          <i class="el-upload__text" style="font-size: 9px;margin-left: 4px;color: #6f7180">选择查询项目: </i>
        </el-form-item>

        <el-form-item style="max-width: 50%">
            <el-dropdown
              size="mini"
              type="primary"
              placement="bottom-start"
              :hide-on-click="true"
              style="width: 87px"
              trigger="click"
              @command="itemHandleCommand" >
            <el-input
              size="mini"
              :readonly= true
              :value="dataForm.searchItem"
              placeholder='某列'
              suffix-icon='el-icon-arrow-down'
              class="el-dropdown-link">
            </el-input>
            <el-dropdown-menu class="project-dropdown" style="z-index: 8; width: 90px" slot="dropdown">
              <div>
                <el-dropdown-item v-for="item in tableHead" :key="item" :command=item>{{item}}</el-dropdown-item>
                <el-dropdown-item divided command='bDefault' >默认</el-dropdown-item></div>
            </el-dropdown-menu>
          </el-dropdown>
        </el-form-item>

        <el-form-item>
          <el-input size="mini" v-model="dataForm.searchItemValue" placeholder="输入想查询的值(可空)"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button size="mini" @click="submitSearch()">提交</el-button>
        </el-form-item>

      </el-form>

      <el-table
      border
      v-model="is_down"
      :data="tableCell"
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;margin-top: 15px">

      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>

      <el-table-column v-for="(index, item) in tableCell[0]" :key="item"
                       :prop= item
                       header-align="center"
                       align="center"
                       :label= item>
      </el-table-column>

<!--      <el-table-column-->
<!--        fixed="right"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="操作">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>-->
<!--          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table></div>
    </el-collapse-transition>
    <!--      :page-size="pageSize"-->
    <el-pagination
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :total="totalCount"
      :page-count="totalPage"
      layout="total, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './config-add-or-update'
export default {
  data () {
    return {
      dataForm: {
        searchItem: '',
        searchItemValue: ''
      },
      table_name: '',
      table_item: '',
      count: 0,
      tableList: [],
      tableHead: [],
      tableCell: [],
      totalCount: 1,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 1,
      dataListLoading: false,
      dataListSelections: [],
      is_down: false,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  activated () {
    this.getTableName()
    // this.getDataList()
  },
  methods: {
    // 获取表名
    getTableName () {
      this.$http({
        url: this.$http.adornUrl('/get/table/names'),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.tableList = data.data
        }
      })
    },
    // 提交结果
    submitSearch () {
      if (this.dataForm.searchItem !== '') {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/get/table/data'),
          method: 'get',
          params: {
            curPage: this.pageIndex,
            tableName: this.table_name,
            fields: this.dataForm.searchItem,
            vals: this.dataForm.searchItemValue
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.tableCell = data.data['tableCell']
            this.pageSize = data.data['pageSize']
            this.totalPage = data.data['totalPage']
            this.totalCount = data.data['totalCount']
            this.dataListLoading = false
          }
        })
      } else {
        this.$message('请先选择查询的列条目!')
      }
    },
    // 点击换成哪个项目
    itemHandleCommand (command) {
      if (command === 'bDefault') {
        this.dataForm.searchItem = ''
      } else {
        this.dataForm.searchItem = command
      }
    },
    // 点击换成哪个表
    handleCommand (command) {
      if (command === 'bDefault') {
        this.table_name = ''
        this.is_down = false
      } else {
        this.$message('选择了' + command + '表')
        this.table_name = command
        this.$http({
          url: this.$http.adornUrl('/get/hms_Chi_Eng/fields'),
          method: 'get',
          params: {tableName:this.table_name}
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.tableHead = data.data
          }
        })
      }
    },
    // 查询handle
    searchHandle () {
      if (this.table_name !== '') {
        // eslint-disable-next-line no-unused-expressions
        this.count = 0
        this.is_down = true
        this.dataListLoading = true
        console.log(this.pageIndex)
        this.$http({
          url: this.$http.adornUrl('/get/table/data'),
          method: 'get',
          params: {
            tableName: this.table_name,
            curPage: this.pageIndex
          }
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.tableCell = data.data['tableCell']
            this.pageSize = data.data['pageSize']
            this.totalPage = data.data['totalPage']
            this.totalCount = data.data['totalCount']
          }
        })
        this.dataListLoading = false
      }
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.searchHandle()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.searchHandle()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/config/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    }
  }
}
</script>
<style lang="scss">
.project-dropdown{
  //!important;
  height: 100px;
  overflow: auto;
}
.project-dropdown::-webkit-scrollbar{
  width: 5px;
  height: 5px;
  background-color: #f5f5f5;
}
.project-dropdown::-webkit-scrollbar-track{
  //-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3)
  border-radius: 10px;
  background-color: #f5f5f5;
}
</style>
